//当前投注选择的单位 key是大小单双，value是万千百十=>1 投注之后要恢复初始值
var cur_select_unit = {
    'big': {},
    'small': {},
    'single': {},
    'double': {}
};
var cur_coin_price = 0; //当前选中的金币面值 投注之后要清零
var cur_total_price = 0; //当前投注的总金额

$(function () {

    //点击万千百十小按钮
    $('.unit_btn').on('click', function () {
        audioAutoPlay('click_btn_music');
        var that = $(this);

        //确定点击区域和单位
        var area = that.parent().data('area');
        var unit = that.data('unit');

        //选中
        if (that.attr('data-click') == '0') {
            that.css('background-position', '0 -0.24rem');
            that.attr('data-click', '1');

            cur_select_unit[area][unit] = 1;
        }
        //取消选中
        else if (that.attr('data-click') == '1') {
            that.attr('style', '');
            that.attr('data-click', '0');

            delete (cur_select_unit[area][unit]);
        }

    });
    //点击金币
    $('.coin').on('click', function () {
        audioAutoPlay('click_btn_music');

        var that = $(this);

        //取消
        if (that.attr('style') != undefined && that.attr('style') != '') {
            that.attr('style', '');
            cur_coin_price = 0;
        }
        //选中
        else {
            $('.coin').attr('style', '');
            //金币上移
            that.css('transform', 'translateY(-10px)');
            that.css('-webkit-transform', 'translateY(-10px)');

            //当前选中的金币面值
            cur_coin_price = parseInt(that.html());
        }

    });
    //投注
    click_btn_action($('.draw_again_btn'));
    $('.draw_again_btn').on('click', function () {

        if (seconds <= 10) {
            if (seconds < 0) {
                set_alert('tip', '该回合结束，请下一局继续投注！');
                return false;

            } else {
                if (seconds > 0 && seconds <= 60) {
                    set_alert('tip', '倒计时60s内不能投注');
                }
            }
            return false;
        }
        //查看此次选择的金币面值
        if (cur_coin_price == 0) {
            set_alert('tip', '请先选择金币');
            return false;
        }


        //金币投放区域
        var gold_area_arr = [];
        //查看已点击的区域
        //console.log(cur_select_unit)
        for (var i in cur_select_unit) {
            //i是大小单双
            var select_num = Object.keys(cur_select_unit[i]).length;
            if (select_num > 0) {
                //获取金币要投放的区域
                gold_area_arr.push(i);
                //计算总金额
                cur_total_price += select_num * cur_coin_price;
                //$('#'+i+'_total').html(cur_total_price);
            }
        }

        if (gold_area_arr.length == 0) {
            set_alert('tip', '请先选择下注的单位');
            return false;
        }

        audioAutoPlay('draw_gold_music');

        //金币类型
        var coin = $('.coin[data-price=' + cur_coin_price + ']');
        var coin_type = coin.data('type');


        var lottery_data = {
            list: cur_select_unit,
            coin: cur_coin_price,
            coin_type: coin_type,
            round_turn: round_turn,
            room_id: room_uuid
        };
        if (!IS_ENTER) {
            set_alert('tip', '请点击空位坐下')
            return false;
        }

        request(GV.url + 'game/addCommonBet', lottery_data, function (data) {
            if (data.ResponseStatus == 0) {
                requestUserInfo();
                //竖向
                if (orientation_portrait_flag) {
                    var coin_x = coin.offset().top;
                    var coin_y = coin.offset().left + coin.height();
                } else {
                    var coin_x = coin.offset().left;
                    var coin_y = coin.offset().top;
                }
                var coin_x = coin.offset().left;
                var coin_y = coin.offset().top;
                //console.log(orientation_portrait_flag);
                //处理投掷数据并丢币
                // console.log(gold_area_arr);
                deal_draw_data(gold_area_arr, coin_x, coin_y, coin_type);
                var gold_info = {
                    area: gold_area_arr,
                    coin_type: coin_type
                };


                //样式恢复
                $('.coin').attr('style', '');
                $('.unit_btn').attr('style', '').attr('data-click', '0');

                //恢复变量
                cur_select_unit = {
                    'big': {},
                    'small': {},
                    'single': {},
                    'double': {}
                };
                cur_coin_price = 0;
                sendDrawCoin(gold_info);


            } else {
                set_alert('tip', data.ResponseMsg);
                //样式恢复
                $('.coin').attr('style', '');
                $('.unit_btn').attr('style', '').attr('data-click', '0');

                //恢复变量
                cur_select_unit = {
                    'big': {},
                    'small': {},
                    'single': {},
                    'double': {}
                };
                cur_coin_price = 0;
            }

        })
        //后端发送请求


    });

});

function handle_drop_coin(data) {

    if(seconds <= 60){
        return false;
    }
    var coin = $('.coin[data-type=' + data['coin_type'] + ']');
    ;
    if ($('.partner_avatar[data-uuid=' + data['from_uuid'] + ']').attr('data-uuid')) {
        coin = $('.partner_avatar[data-uuid=' + data['from_uuid'] + ']');
    }
    audioAutoPlay('draw_gold_music');
    if (orientation_portrait_flag) {
        var coin_x = coin.offset().top;
        var coin_y = coin.offset().left + coin.height();
    } else {
        var coin_x = coin.offset().left;
        var coin_y = coin.offset().top;
    }
    var coin_x = coin.offset().left;
    var coin_y = coin.offset().top;
    //处理投掷数据并丢币
    var gold_area_arr = data['area']
    var coin_type = data['coin_type'];
    deal_draw_data(gold_area_arr, coin_x, coin_y, coin_type);
}

function request(url, data, callback) {
    data['token'] = token;
    $.ajax({
        url: url,
        data: data,
        type: 'post',
        dataType: 'json',
        success: function (data) {
            if (typeof callback == 'function') {
                callback(data);
            }
        }
    })

}

//分析投注数据然后投金币
//json是投放区域的数组，如['big','small']
//ori_x，ori_y 投掷的金币的初始位置
//coin_type金币类型
function deal_draw_data(json, ori_x, ori_y, coin_type) {
    if (json.length == 0) {
        return;
    }
    for (var i = 0; i < json.length; i++) {
        var html = '<img src="' + GV.img_url + 'img/coin/' + coin_type + '.png"/>';
        $('.draw_golds_div').append(html);
        //获取img
        var img = $('.draw_golds_div').find('img').eq($('.draw_golds_div >img').length - 1);
        //投掷金币
        draw_gold(img, ori_x, ori_y, json[i]);
    }
    audioAutoPlay('draw_gold_music');
}

//随机投掷金币
function draw_gold(dom, ori_x, ori_y, area_name) {

    //获取投币区域范围
    var result = get_random_offset($('.draw_' + area_name + '_area'));
    //	console.log(result);
    //定位到初始位置
    //竖向
    if (orientation_portrait_flag) {
        dom.css({
            'left': ori_y + 'px',
            'top': ori_x + 'px'
        });
        dom.animate({
            'left': result.bottom + 'px',
            'top': result.left + 'px'
        }, 300);
    } else {
        dom.css({
            'top': ori_y + 'px',
            'left': ori_x + 'px'
        });
        dom.animate({
            'top': result.top + 'px',
            'left': result.left + 'px'
        }, 300);
    }

}

//获取区域的坐标
function get_random_offset(dom) {

    var area_width = dom.width() - 26; //减去小图标的宽度防止超出
    var area_height = dom.height() - 26;
    if (orientation_portrait_flag) {

        var area_bottom = dom.offset().left + area_height;
        var area_left = dom.offset().top;
        return {
            bottom: getRandom(area_bottom - area_height, area_bottom),
            left: getRandom(area_left, area_left + area_width)
        };
    } else {

        var area_left = dom.offset().left;
        var area_top = dom.offset().top;
        return {
            top: getRandom(area_top, area_top + area_height),
            left: getRandom(area_left, area_left + area_width)
        };
    }

}

function sendDrawCoin(data) {
    var gold_data = {
        'action': 'draw_coin',
        'content': {
            'area': data.area,
            coin_type: data.coin_type
        }
    };

    send(JSON.stringify(gold_data));


}
